//
//  TKL_MySetInfoVC.m
//  TKL_EnPro
//
//  Created by 雅恩三河科技 on 2018/12/19.
//  Copyright © 2018年 MrZhang. All rights reserved.
//

//
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                  \\`.
//        .'//                     |                     \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
//    .'//.-"                 `-.  |  .-'                 "-.\\`.
//  .'//______.============-..   \ | /   ..-============.______\\`.
//.'______________________________\|/______________________________`.
//
//

#import "TKL_MySetInfoVC.h"

@interface TKL_MySetInfoVC ()<UIActionSheetDelegate,TZImagePickerControllerDelegate>
@property(nonatomic,strong)UIImage* myImage;
@end

@implementation TKL_MySetInfoVC
-(void)initilization
{
    self.isGroup=YES;
    self.navigationItem.title = @"个人信息";
    self.extendedLayoutIncludesOpaqueBars = YES;
    //self.navigationController.navigationBar.shadowImage = [[UIImage new]rt_darkenWithLevel:0.3f];
    [self.view addSubview:self.baseTable];

    self.baseTable.contentInset = UIEdgeInsetsMake([self topShelterHeight], 0, [self bottomShelterHeight], 0);
    [self.baseTable setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [self.baseTable setSeparatorColor: Main_Color_BG];
    [self.baseTable setBackgroundColor:Main_Color_BG];
    [self.baseTable setTintColor:Main_Color_BG];
    @WeakSelf;
    
    [self viewModelBand];
    [self reloadData];
    [selfp setRightBarButtonWithTitle:@"保存" titleColor:HEXCOLOR(@"#ff7890") withBlock:^{
        [selfp requestCityData];
    }];
}
-(void)viewModelBand
{
    @WeakSelf;
//    GCBaseObservationModel *observation = [GCBaseObservationModel new];
//    observation.observation = self.viewModel;
//    observation.keyPath = @"datas";
//    observation.handler = ^(NSString *keyPath) {
//        [selfp reloadView];
//    };
//    [self registObservation:observation];
}
-(void)reloadData
{
    
}
-(void)reloadView
{
    [self.baseTable reloadData];
}
#pragma mark - life
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initilization];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
#pragma mark - delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row==0)
    {
        static NSString *CellID = @"CellID0";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        [cell.contentView removeAllSubviews];
        cell.height=WFCGFloatY(80);
        cell.textLabel.text=@"头像";
        
        UIImageView* headIV=[UIImageView new];
        headIV.x=SCREEN_WIDTH-WFCGFloatX(15)-WFCGFloatY(60);
        headIV.y=WFCGFloatY(10);
        headIV.width=WFCGFloatY(60);
        headIV.height=WFCGFloatY(60);
        if ([TKL_LoginViewModel shareInterface].headImage)
        {
            headIV.image=[TKL_LoginViewModel shareInterface].headImage;
        }else
        {
            [headIV sd_setImageWithURL:[NSURL URLWithString:[WFFunctions WFCheckEmptyBackStr:[_passInfo objectForKey:@"avatar"]]] placeholderImage:DefaultAvatarMImage];
        }
        
        [cell.contentView addSubview:headIV];
        [WFFunctions WFUIaddbordertoView:headIV radius:4 width:0 color:[UIColor whiteColor]];

        return cell;
        
    }
    static NSString *CellID = @"CellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    }
    [cell.contentView removeAllSubviews];
    NSArray* arr=@[@"昵称",@"性别"];
    cell.textLabel.text=arr[indexPath.row-1];
   //
    cell.detailTextLabel.textColor=HEXCOLOR(@"#666666");
    cell.height=WFCGFloatY(50);
    if (indexPath.row==1) {
        cell.detailTextLabel.text=[_passInfo objectForKey:@"nickname"];
    }else
    {
        if ([[_passInfo objectForKey:@"gender"] integerValue]==2)
        {
            cell.detailTextLabel.text=@"男";
        }else
        {
            cell.detailTextLabel.text=@"女";
        }
        
    }
    //
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return WFCGFloatY(45);
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = (UITableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    CGFloat height = cell.height;
    return height;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    @WeakSelf;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row==0)
    {
        TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
        imagePickerVc.allowTakePicture=NO;
        imagePickerVc.allowPickingOriginalPhoto=NO;
        // You can get the photos by block, the same as by delegate.
        // 你可以通过block或者代理，来得到用户选择的照片.
        [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
            // [[TFInfoViewModel shareInterface] uploadImage:photos[0]];
            [FTIndicator showProgressWithMessage:@"正在上传头像"];
            
            NSData *data = UIImagePNGRepresentation(photos[0]);
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [FTIndicator showSuccessWithMessage:@"上传成功"];
                selfp.myImage=[photos objectAtIndex:0];
                [TKL_LoginViewModel shareInterface].headImage=[photos objectAtIndex:0];
                [selfp.baseTable reloadData];
            });
            
        }];
        [self presentViewController:imagePickerVc animated:YES completion:nil];
    }
    
    if (indexPath.row==1)
    {
        NSString *title = @"昵称";
        NSString *hold1 = @"请输入昵称";
        TYAlertView *alertView = [TYAlertView alertViewWithTitle:title message:nil];
        [alertView addAction:[TYAlertAction actionWithTitle:@"取消" style:TYAlertActionStyleCancel handler:^(TYAlertAction *action) {
            NSLog(@"%@",action.title);
        }]];
        
        [alertView addAction:[TYAlertAction actionWithTitle:@"确认" style:TYAlertActionStyleDestructive handler:^(TYAlertAction *action) {
            UITextField *mintf = alertView.textFieldArray[0];
            if (mintf.text.length > 0) {
                [selfp.passInfo setObject:mintf.text forKey:@"nickname"];
                [selfp.baseTable reloadData];
                
            }else{
                [FTToastIndicator showToastMessage:@"请填写完整"];
            }
        }]];
        
        [alertView addTextFieldWithConfigurationHandler:^(UITextField *textField) {
            textField.placeholder = hold1;
            textField.keyboardType = UIKeyboardTypeDefault;
            //[textField becomeFirstResponder];
        }];
        alertView.clickedAutoHide = YES;
        TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:alertView preferredStyle:TYAlertControllerStyleAlert];
         alertController.alertViewOriginY=[self topShelterHeight]+60;
        //alertController.alertViewOriginY = 60;
        [self presentViewController:alertController animated:YES completion:nil];
    }
    
    if (indexPath.row==2)
    {
        UIActionSheet * c=[[UIActionSheet alloc] initWithTitle:@"性别" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"男",@"女", nil];
        [c showActionSheetWithCompleteBlock:^(NSInteger buttonIndex) {

            if (buttonIndex==0) {
                [selfp.passInfo setObject:@"2" forKey:@"gender"];
            }
            if (buttonIndex==1) {
                [selfp.passInfo setObject:@"1" forKey:@"gender"];
            }
            [selfp.baseTable reloadData];
        }];
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView* view=[UIView new];
    view.backgroundColor=Main_Color_BG;
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5;
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"--%ld",buttonIndex);
}
-(void)requestCityData
{
    @WeakSelf;
    //
    [FTIndicator showProgressWithMessage:@"正在保存"];
    NSMutableDictionary* para=[NSMutableDictionary new];
    [para setObject:[TKL_LoginViewModel shareInterface].infoModel.uuid forKey:@"uuid"];
    [para setObject:[_passInfo objectForKey:@"avatar"] forKey:@"avatar"];
    [para setObject:[_passInfo objectForKey:@"nickname"] forKey:@"nickname"];
    [para setObject:[_passInfo objectForKey:@"gender"] forKey:@"gender"];
    [para setObject:@"无" forKey:@"signature"];

    [[InterfaceManager shareInterface] requestAllInterface:@"http://101.201.171.131:8080/rest/api/user/update" para:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        [FTIndicator showSuccessWithMessage:@"保存成功"];
        [selfp.navigationController popViewControllerAnimated:YES];
        
    }];
}
@end


/*
 11111111111111111111111111111111111111001111111111111111111111111
 11111111111111111111111111111111111100011111111111111111111111111
 11111111111111111111111111111111100001111111111111111111111111111
 11111111111111111111111111111110000111111111111111111111111111111
 11111111111111111111111111111000000111111111111111111111111111111
 11111111111111111111111111100000011110001100000000000000011111111
 11111111111111111100000000000000000000000000000000011111111111111
 11111111111111110111000000000000000000000000000011111111111111111
 11111111111111111111111000000000000000000000000000000000111111111
 11111111111111111110000000000000000000000000000000111111111111111
 11111111111111111100011100000000000000000000000000000111111111111
 11111111111111100000110000000000011000000000000000000011111111111
 11111111111111000000000000000100111100000000000001100000111111111
 11111111110000000000000000001110111110000000000000111000011111111
 11111111000000000000000000011111111100000000000000011110001111111
 11111110000000011111111111111111111100000000000000001111100111111
 11111111000001111111111111111111110000000000000000001111111111111
 11111111110111111111111111111100000000000000000000000111111111111
 11111111111111110000000000000000000000000000000000000111111111111
 11111111111111111100000000000000000000000000001100000111111111111
 11111111111111000000000000000000000000000000111100000111111111111
 11111111111000000000000000000000000000000001111110000111111111111
 11111111100000000000000000000000000000001111111110000111111111111
 11111110000000000000000000000000000000111111111110000111111111111
 11111100000000000000000001110000001111111111111110001111111111111
 11111000000000000000011111111111111111111111111110011111111111111
 11110000000000000001111111111111111100111111111111111111111111111
 11100000000000000011111111111111111111100001111111111111111111111
 11100000000001000111111111111111111111111000001111111111111111111
 11000000000001100111111111111111111111111110000000111111111111111
 11000000000000111011111111111100011111000011100000001111111111111
 11000000000000011111111111111111000111110000000000000011111111111
 11000000000000000011111111111111000000000000000000000000111111111
 11001000000000000000001111111110000000000000000000000000001111111
 11100110000000000001111111110000000000000000111000000000000111111
 11110110000000000000000000000000000000000111111111110000000011111
 11111110000000000000000000000000000000001111111111111100000001111
 11111110000010000000000000000001100000000111011111111110000001111
 11111111000111110000000000000111110000000000111111111110110000111
 11111110001111111100010000000001111100000111111111111111110000111
 11111110001111111111111110000000111111100000000111111111111000111
 11111111001111111111111111111000000111111111111111111111111100011
 11111111101111111111111111111110000111111111111111111111111001111
 11111111111111111111111111111110001111111111111111111111100111111
 11111111111111111111111111111111001111111111111111111111001111111
 11111111111111111111111111111111100111111111111111111111111111111
 11111111111111111111111111111111110111111111111111111111111111111
 
 
 */
